package wqh.controller;

import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import wqh.pojo.MapRet;
import wqh.service.DtAlarmEventConfirmService;
import wqh.service.DtAlarmEventService;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * Created by cfs on 2016/12/20.
 */
@RestController
@CrossOrigin(origins = "*")
@RequestMapping("/water/dt_analysis_report")
public class DtAnalysisReportController {
    private static org.slf4j.Logger logger = LoggerFactory.getLogger(DtAnalysisReportController.class);

    @Autowired
    private DtAlarmEventService dtAlarmEventService;

    @Autowired
    private DtAlarmEventConfirmService dtAlarmEventConfirmService;

    @RequestMapping(value = "/getAll",method = RequestMethod.GET)
    public Object getALl(@RequestParam(value = "year",required = false,defaultValue = "0") int year_input)
    {
        try{
            int year;
            if(year_input == 0){
                LocalDate today = LocalDate.now();
                year = today.getYear();
            }else{
                year = year_input;
            }
            List<Object> objectList = new ArrayList<Object>();
            for(int i = 1;i<=12;i++){
                if(i%2 != 0 && i<9 || i%2 == 0 && i>=8){
                    String start = year + "-" + i + "-" + 1;
                    String end = year + "-" + i + "-" + 30;
                    objectList.add(getCountMap(i,start,end));
                }else if(i==2){
                    String start = year + "-" + i + "-" + 1;
                    String end = null;
                    if(year%4==0 && year % 100 !=0){
                        end = year + "-" + i + "-" + 29;
                    }else {
                        end = year + "-" + i + "-" + 28;
                    }
                    objectList.add(getCountMap(i,start,end));
                }else {
                    String start = year + "-" + i + "-" + 1;
                    String end = year + "-" + i + "-" +29;
                    objectList.add(getCountMap(i,start,end));
                }
            }
            return MapRet.DATA(objectList);
        }catch (Exception e){
            e.printStackTrace();
            logger.error("获取报表数据异常："+e.getMessage());
            return MapRet.EXCEL_ERROR();
        }
    }

    @RequestMapping(value = "/getMonth",method = RequestMethod.POST)
    public Object getAll(@RequestParam(value = "year",required = true) int year_input,
                         @RequestParam(value = "month",required = true) int month_input)
    {
        int monthLenght = 0;
        List<Object> objectList = new ArrayList<Object>();
        try {
            if(month_input%2 != 0 && month_input<9 || month_input%2 == 0 && month_input >=8){
                monthLenght = 31;
            }else if(month_input == 2){
                if(year_input%4 == 0 && year_input%100 != 0){
                    monthLenght = 29;
                }else {
                    monthLenght = 28;
                }
            }else {
                monthLenght = 30;
            }
            for(int i = 1;i<=monthLenght;i++){
                String start = year_input + "-" + month_input + "-" + i;
                String end = year_input + "-" + month_input + "-" + i;
                objectList.add(getCountMap2(i,start,end));
            }
            return MapRet.DATA(objectList);
        }catch (Exception e){
            e.printStackTrace();
            logger.error("获取月份记录出错："+e.getMessage());
            return MapRet.SQL_ERROR();
        }
    }

    public Map<String,Object>getCountMap2(int day,String start,String end){
        try{
            int startUnix = new Long((new java.text.SimpleDateFormat("yyy-MM-dd HH:mm:ss").parse(start + " 00:00:00").getTime() / 1000)).intValue();
            int endUnix = new Long((new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(end + " 23:59:59").getTime() / 1000)).intValue();
            Integer day_alarm = dtAlarmEventService.countByTimeRange(startUnix,endUnix);
            Integer day_confirm = dtAlarmEventConfirmService.countByTimeRange(startUnix,endUnix);
            Map<String ,Object> stringObjectMap = new HashMap<String, Object>();
            stringObjectMap.put("day",day);
            stringObjectMap.put("day_alarm" ,day_alarm);
            stringObjectMap.put("day_confirm",day_confirm);
            stringObjectMap.put("day_all",day_alarm + day_confirm);
            return stringObjectMap;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    public Map<String,Object> getCountMap(int month,String start,String end){
        try{
            int startUnix = new Long ((new java.text.SimpleDateFormat("yyyy-MM-dd").parse(start).getTime() / 1000)).intValue();
            int endUnix = new Long((new java.text.SimpleDateFormat("yyyy-MM-dd").parse(end).getTime() / 1000)).intValue();
            Integer month_alarm = dtAlarmEventService.countByTimeRange(startUnix,endUnix);
            Integer month_confirm = dtAlarmEventConfirmService.countByTimeRange(startUnix,endUnix);
            double month_rate;
            if((month_alarm + month_confirm) == 0){
                month_rate = 0;
            }else {
                month_rate = (double)month_confirm / (month_alarm + month_confirm);
            }
            Map<String,Object>stringObjectMap = new HashMap<String, Object>();
            stringObjectMap.put("month",month);
            stringObjectMap.put("month_alarm",month_alarm);
            stringObjectMap.put("month_confirm",month_confirm);
            stringObjectMap.put("month_all",month_alarm + month_confirm);
            stringObjectMap.put("month_rate",month_rate);
            return stringObjectMap;
        }catch (Exception e){
            e.printStackTrace();
            return  null;
        }
    }

}
